/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head = new ListNode();
        ListNode pos = head;
        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                pos.next = l1;
                pos = l1;
                l1 = l1.next;
            } else {
                pos.next = l2;
                pos = l2;
                l2 = l2.next;
            }
        }
        while (l1 != null) {
            pos.next = l1;
            pos = l1;
            l1 = l1.next;
        }
        while (l2 != null) {
            pos.next = l2;
            pos = l2;
            l2 = l2.next;
        }
        return head.next;
    }
}